package com.alibaba.citrus.ots.sales.plan.facade.service.setting;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.common.exception.FacadeException;
import com.alibaba.citrus.ots.sales.plan.api.setting.SalesPlanStrategyWriteService;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.PageQuerySalesPlanStrategyRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QuerySalesPlanStrategyRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.SalesOrgRatioDTO;
import com.alibaba.citrus.ots.sales.plan.functions.planstrategy.PlanStrategyEntity;
import com.alibaba.citrus.ots.sales.plan.repository.SalesPlanStrategyRepository;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanStrategySDO;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SalesPlanStrategyWriteServiceImpl implements SalesPlanStrategyWriteService {

    @Resource
    private SalesPlanStrategyRepository salesPlanStrategyRepository;

    @FacadeInvoker
    @Override
    public Result<String> createPlanStrategy(SalesPlanStrategySDO planStrategySDO) {
        // 业务规则校验
        checkBizRule(planStrategySDO);
        // 一个业务单元一个类型只能存在一个
        checkIfRepeat(planStrategySDO);
        // 构造实体
        PlanStrategyEntity planStrategyEntity = PlanStrategyEntity.of(planStrategySDO);
        String lastInsertId = planStrategyEntity.save();
        return Result.success(lastInsertId);
    }

    @FacadeInvoker
    @Override
    public Result<Integer> editSalesPlanStrategy(SalesPlanStrategySDO salesPlanStrategySDO) {
        // 业务规则校验
        checkBizRule(salesPlanStrategySDO);
        QuerySalesPlanStrategyRequest querySalesPlanStrategyRequest = new QuerySalesPlanStrategyRequest();
        querySalesPlanStrategyRequest.setId(salesPlanStrategySDO.getId());
        SalesPlanStrategySDO strategySDO = salesPlanStrategyRepository.querySalesPlanStrategy(querySalesPlanStrategyRequest);
        if (null == strategySDO) {
            throw new FacadeException(ErrorMessage.code("OTS-02-004-01-16-014",  salesPlanStrategySDO.getId()));
        }
        PlanStrategyEntity planStrategyEntity  = PlanStrategyEntity.of(salesPlanStrategySDO);
        Integer count = planStrategyEntity .doEdit();
        return Result.success(count);
    }

    private void checkBizRule(SalesPlanStrategySDO planStrategySDO) {
        if (StringUtils.isBlank(planStrategySDO.getFeatures())) {
            return;
        }
        List<SalesOrgRatioDTO> ratioDTOList = JSONArray.parseArray(planStrategySDO.getFeatures(), SalesOrgRatioDTO.class);
        if (CollectionUtils.isEmpty(ratioDTOList)) {
            throw new FacadeException("OTS-02-004-01-16-019");
        }
        int sum = ratioDTOList.stream().mapToInt(SalesOrgRatioDTO::getRatio).sum();
        if (sum != 10000) {
            throw new FacadeException("OTS-02-004-01-16-020");
        }
    }

    private void checkIfRepeat(SalesPlanStrategySDO planStrategySDO) {
        PageQuerySalesPlanStrategyRequest request = new PageQuerySalesPlanStrategyRequest();
        request.setOrgBusinessCode(planStrategySDO.getOrgBusinessCode());
        request.setType(planStrategySDO.getType());
        request.setStart(0);
        request.setLimit(1);
        PageResult<List<SalesPlanStrategySDO>> pageResult = salesPlanStrategyRepository.pageQuery(request);
        if (null != pageResult && pageResult.getTotal() > 0) {
            throw new FacadeException(ErrorMessage.code("OTS-02-004-01-16-015"));
        }
    }
}
